package com.ass.rocketmq.producer;

import com.ass.rocketmq.constant.MQTopic;
import com.ass.rocketmq.constant.ProducerGroupName;
import com.common.core.text.UUID;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.acl.common.AclClientRPCHook;
import org.apache.rocketmq.acl.common.SessionCredentials;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.text.SimpleDateFormat;
import java.util.Date;

@Log4j2
public class Producer {

    //生产组
    public static final String PRODUCER_GROUP = "ProducerGroupName";
    //nameserver地址 集群用分号隔开127.0.0.1:9876;127.0.0.1:9876
    public static final String DEFAULT_NAMESRVADDR = "192.168.23.104:9876;192.168.23.102:9876";
    public final static MQTopic mqTopic =   MQTopic.TEST;;
    public static final String active = "dev";
    public static final String namespace = "ly";
    public static final String accessKey = "RocketMQ";
    public static final String secretKey = "123456";






    //同步方式
    public static void main(String[] args)  {
        test2();
    }

    @SneakyThrows
    private static void test() {
        DefaultMQProducer producer = new DefaultMQProducer(namespace,
                ProducerGroupName.DEFAULT_GROUP_NAME.name(),
                new AclClientRPCHook(new SessionCredentials(accessKey, secretKey)));
        producer.setNamesrvAddr(DEFAULT_NAMESRVADDR);
        producer.setVipChannelEnabled(false);
        producer.setRetryTimesWhenSendAsyncFailed(10);


        Message message = messageBuild();

        producer.start();

        producer.send(message);

        producer.shutdown();

    }

    //官网例子
    @Deprecated
    @SneakyThrows
    private static void test1() {
        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName", true);
        producer.setNamesrvAddr(DEFAULT_NAMESRVADDR);
        producer.start();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Message msg = new Message("TopicTest",
                "TagA",
                UUID.randomUUID().toString(),
                sdf.format(new Date()).getBytes());
        SendResult sendResult = producer.send(msg);
        System.out.printf("%s%n", sendResult);
    }


    @SneakyThrows
    private static void test2()  {
        DefaultMQProducer producer = new DefaultMQProducer(namespace,
                ProducerGroupName.DEFAULT_GROUP_NAME.name(),
                new AclClientRPCHook(new SessionCredentials(accessKey, secretKey)),
                true, "");
        producer.setNamesrvAddr(DEFAULT_NAMESRVADDR);
        producer.start();

        Message message = messageBuild();
        SendResult sendResult = producer.send(message);
        System.out.printf("%s%n", sendResult);
        producer.shutdown();
    }



    private static Message messageBuild(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Message message = new Message();
        message.setTopic(active + "_" + mqTopic.topic);
        message.setTags(active + "_" + mqTopic.tag);
        message.setBody(sdf.format(new Date()).getBytes());
        message.setKeys(UUID.randomUUID().toString());
        message.setDelayTimeLevel(mqTopic.delayTimeLevel);
        return message;
    }
}
